package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.ExportProductDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.PackingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service(timeout = 300000)
public class PackingServiceImpl implements PackingService {

    @Autowired
    private PackingDao packingDao;
    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ExportProductDao exportProductDao;

    @Override
    public PageInfo<Packing> findByPage(PackingExample packingExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Packing> list = packingDao.selectByExample(packingExample);
        return new PageInfo<>(list);
    }

    @Override
    public Packing PageBean(String id) {

        //分割
        String[] array = id.split(",");
        List<String> exportIdList = Arrays.asList(array);
        //根据报运单id查询合同号
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andIdIn(exportIdList);
        List<Export> exportList = exportDao.selectByExample(exportExample);
        //保存合同号
        String exportNos = "";
        if (exportList != null && exportList.size() > 0 ){
            for (Export export : exportList) {
                exportNos += export.getCustomerContract() + " ";
                //修改报运单状态
                export.setState(3);
                exportDao.updateByPrimaryKeySelective(export);
            }
        }
        Packing packing = new Packing();
        //设置报运合同
        packing.setExportNos(exportNos);

        Double totalVolume = 0d;
        Double netWeights = 0d;
        Double grossWeights = 0d;
        Double packingMoney = 0d;
        // 查询报运合同号、总体积、总毛重和计算装箱费用
        ExportExample example = new ExportExample();
        example.createCriteria().andIdIn(exportIdList);
        List<Export> exportList1 = exportDao.selectByExample(example);
        if (exportList1 != null && exportList1.size() > 0) {
            for (Export export : exportList1) {
                //packing.setExportNos(packing.getExportNos() + export.getCustomerContract());

                grossWeights += export.getGrossWeights();
                packingMoney += export.getGrossWeights() * 520;
            }
        }
        // 计算总净重
        ExportProductExample productExample = new ExportProductExample();
        productExample.createCriteria().andExportIdIn(exportIdList);
        List<ExportProduct> exportProductList = exportProductDao.selectByExample(productExample);
        if (exportProductList != null && exportProductList.size() > 0) {
            for (ExportProduct exportProduct : exportProductList) {
                if (exportProduct.getNetWeight() != null) {
                    totalVolume += exportProduct.getSizeWidth() * exportProduct.getSizeLength() * exportProduct.getSizeHeight();
                    netWeights += exportProduct.getNetWeight();
                }
            }
        }

        //保存体积到装箱单
        packing.setTotalVolume(totalVolume);
        //保存总净重到装箱单
        packing.setNetWeight(netWeights);
        //保存总毛重到装箱单
        packing.setGrossWeights(grossWeights);
        //保存装箱费用到装箱单
        packing.setPackingMoney(packingMoney);
        //添加装箱单
        //packingDao.insert(packing);
        return packing;
    }

    @Override
    public Packing findById(String id) {
        return packingDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Packing packing) {
        //设置装箱单id
        packing.setPackingListId(UUID.randomUUID().toString());
        //设置状态
        packing.setState(0);
        packingDao.insertSelective(packing);
    }

    @Override
    public void update(Packing packing) {
        packingDao.updateByPrimaryKeySelective(packing);
    }

    @Override
    public void delete(String id) {
        Packing packing = packingDao.selectByPrimaryKey(id);
        String exportIds = packing.getExportIds();
        ExportExample example = new ExportExample();
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(exportIds);
        List<Export> exportList = exportDao.selectByExample(example);
        for (Export export : exportList) {
            export.setState(2);
            exportDao.updateByPrimaryKeySelective(export);
        }
        packingDao.deleteByPrimaryKey(id);
    }
}
